package com.zmx.common.common.service;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

/**
 * @Description: 生成/发布基础service类
 * @ClassName: BaseService
 * @Author zhaomxr
 * @Date 2021-08-30 10:47
 */
public class SenderBaseService<T> {

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 用于消息队列发送消息
     *
     * @param exchange 交换器
     * @param t        发送内容
     */
    public void senderForBean(String exchange, T t) {
        if (StringUtils.isEmpty(exchange) || t == null) return;
        amqpTemplate.convertAndSend(exchange, t);
    }

    /**
     * 用于消息队列发送消息
     *
     * @param exchange 交换器
     * @param object   发送内容
     */
    public void sender(String exchange, Object object) {
        if (StringUtils.isEmpty(exchange) || object == null) return;
        amqpTemplate.convertAndSend(exchange, object);
    }

    /**
     * 用于消息队列发送消息
     *
     * @param exchange 交换器
     * @param key      指定路由key
     * @param object   发送内容
     */
    public void sender(String exchange, String key, Object object) {
        if (StringUtils.isEmpty(exchange) || StringUtils.isEmpty(key) || object == null) return;
        amqpTemplate.convertAndSend(exchange, key, object);
    }
}
